package com.xiaowen.config.security;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * Created with IntelliJ IDEA.
 * User: yf-wenhao
 * Date: 2024/9/18
 * Time: 下午4:59
 */
public class SmsCodeAuthenticationToken extends AbstractAuthenticationToken {

    //认证前
    public SmsCodeAuthenticationToken(Object principal,String orgName) {
        super(null);
        this.principal = principal;
        this.orgName = orgName;
        this.setAuthenticated(false);
    }

    //认证后
    public SmsCodeAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities, String orgName) {
        super(authorities);
        this.principal = principal;
        this.orgName = orgName;
        super.setAuthenticated(true);
    }
    //存放认证信息，认证前存放的是手机号，认证之后UserDetails
    private final Object principal;
    private final String orgName;

    public String getOrgName() {
        return orgName;
    }

    @Override
    public Object getCredentials() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }



    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        } else {
            super.setAuthenticated(false);
        }
    }
}
